package club.beimeng.emailserver2.utils;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@Data
public class R {
    // json返回值类型
    private static final int SUCCESS = 200;
    private static final int ERROR = 201;

    @ApiModelProperty("是否成功")
    private boolean success;

    @ApiModelProperty("返回状态码")
    private Integer code;

    @ApiModelProperty("返回提示信息")
    private String message;

    @ApiModelProperty("返回的数据")
    private Map<String,Object> data = new HashMap<>();

    private R(){}; // 构造参数私有化

    /*成功静态方法*/
    public static R ok(){
        R R = new R();
        R.setSuccess(true);
        R.setCode(SUCCESS);
        R.setMessage("成功");
        return R;
    }
    /*失败静态方法*/
    public static R error(){
        R R = new R();
        R.setSuccess(false);
        R.setCode(ERROR);
        R.setMessage("失败");
        return R;
    }

    // 实现链式变成
    public R success(boolean value){
        this.setSuccess(value);
        return this;
    }

    public R message(String message){
        this.setMessage(message);
        return this;
    }

    public R code(Integer code){
        this.setCode(code);
        return this;
    }

    public R data(String key,Object value){
        this.data.put(key,value);
        return this;
    }
}
